package primary.math;

/**
 * @Author Elephas
 * @Date 2022/1/10
 **/
public class RomanToInt {

    /*
        解法一：从左向右优先匹配
     */
    public static void main(String[] args) {
        final String s = "HELLOWORLD";
        final String TEST01 = "IX";
        romanToInt(TEST01);
    }
    public static int romanToInt(String s){
        if(s.length() == 0){
            return 0;
        }else if(s.length() == 1){
            return romanToIntTable(s);
        }else{
            int temp = romanToIntTable(s.substring(0,2));
            if(temp != 0){
                return temp + romanToInt(s.substring(2));
            }else{
                temp = romanToIntTable(s.substring(0,1));
                return temp + romanToInt(s.substring(1));
            }
        }
    }

    public static int romanToIntTable(String str){
        int ans = 0;
        switch (str){
            case "I": ans = 1; break;
            case "V": ans = 5; break;
            case "X": ans = 10; break;
            case "L": ans = 50; break;
            case "C": ans = 100; break;
            case "D": ans = 500; break;
            case "M": ans = 1000; break;
            case "IV": ans = 4; break;
            case "IX": ans = 9; break;
            case "XL": ans = 40; break;
            case "XC": ans = 90; break;
            case "CD": ans = 400; break;
            case "CM": ans = 900; break;
            default:ans=0;
        }
        return ans;
    }
}
